package cn.simple.manager.configuration;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DynamicDataSource extends AbstractRoutingDataSource {
    private final static     Map<Object, Object> DATASOURCEMAP = new ConcurrentHashMap<>();
    DynamicDataSource (Map<String, DataSource> sourceMap){
        super();
        loadSource(sourceMap);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceHolder.getHolder();
    }

    @Override
    public void setTargetDataSources(Map<Object, Object> targetDataSources) {
        super.setTargetDataSources(targetDataSources);
    }

    void loadSource(Map map){
        setTargetDataSources(map);
        setDefaultTargetDataSource(((Map.Entry)map.entrySet().iterator().next()).getValue());
    }
}
